<template>
	<view class="search">
		<!-- 搜索框区域 -->
		<view class="search-box">
			<uni-search-bar class="search-bar" @input="input" :radius="100" cancelButton="none" :focus="true"
				placeholder="请输入搜索内容"></uni-search-bar>
		</view>
		<!-- 搜索数据展示区域 -->
		<view class="search-show-box" v-if="keyWords !== ''">
			<view class="show-item" v-for="item in searchList" :key="item.goods_id" @click="goToDetails(item.goods_id)">
				<text class="show-text">{{item.goods_name}}</text>
				<uni-icons type="right" size="17" color="gray"/>
			</view>
		</view>
		<!-- 搜索历史区域 -->
		<view class="search-history-box" v-else>
			<view class="history-box-header">
				<text class="header-title">搜索历史</text>
				<uni-icons type="trash" size="20" color="#333" @click="clearHistorySearch"/>
			</view>
			<view class="history-box-body">
				<view class="tag-item" v-for="(item,index) in localKeyWordsArr" :key="index" @click="goToLists(item)">
					<uni-tag :text="item" inverted></uni-tag>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 搜索关键字
				keyWords: '',
				// 关键字数组本地存储
				localKeyWordsArr: JSON.parse(uni.getStorageSync('local_search') || '[]'),
				// 定时器ID
				timer: null,
				// 关键字请求数据
				searchList: []
			};
		},
		methods: {
			// 搜索框输入触发事件
			input(text) {
				// 函数防抖
				clearTimeout(this.timer);
				this.timer = setTimeout(() => {
					this.keyWords = text;
					this.localKeyWordsArr.unshift(text);
					// 数组去重
					this.localKeyWordsArr = [...new Set(this.localKeyWordsArr)];
					// 发送请求
					this.getSearchList(this.keyWords);
					// 搜索历史本地存储
					this.localHistorySearch(this.localKeyWordsArr);
				}, 500)
			},
			// 发送请求获取请求数据
			async getSearchList(keyWords) {
				if (keyWords == '') return uni.$showToast('请输入内容');
				const {
					data: res
				} = await uni.$http.get(`/api/public/v1/goods/qsearch?query=${keyWords}`);
				this.searchList = res.message;
			},
			// 跳转到详情页
			goToDetails(id){
				uni.navigateTo({
					url:`/subpage/details/details?goods_id=${id}`
				})
			},
			// 搜索历史进行本地存储
			localHistorySearch(historyData){
				historyData = JSON.stringify(historyData);
				uni.setStorageSync('local_search', historyData);
			},
			// 清空搜索历史
			clearHistorySearch(){
				this.localKeyWordsArr = [];
				uni.setStorageSync('local_search', '[]');
			},
			// 点击搜索历史跳转列表页
			goToLists(keyword){
				uni.navigateTo({
					url:`/subpage/lists/lists?query=${keyword}`
				})
			}
		}
	}
</script>

<style lang="less">
	// 搜索框区域
	.search {
		.search-box {
			position: sticky;
			top: 0;
			z-index: 999;
			width: 100%;
			background-color: #c00000;

			.search-bar {
				width: 100%;
			}
		}
		// 搜索数据列表展示区域
		.search-show-box{
			padding: 0 5px;
			.show-item{
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;
				height: 90rpx;
				background-color: #fff;
				border-bottom: 1px solid #efefef;
				.show-text{
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis; 
					font-size: 24rpx;
				}
			}
		}
		// 搜索历史区域
		.search-history-box{
			width: 100%;
			padding: 0 5px;
			box-sizing: border-box;
			background-color: #fff;
			.history-box-header{
				width: 100%;
				height: 80rpx;
				border-bottom: 1px solid #efefef;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.header-title{
					font-size: 30rpx;
					color: #000000;
				}
			}
			.history-box-body{
				display: flex;
				flex-wrap: wrap;
				.tag-item{
					margin-top: 5px;
					margin-right: 5px;
				}
			}
		}
	}
</style>
